package com.wap.framework.pagination;

import java.sql.Types;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;

import com.wap.framework.util.Constants;

public class PaginationHelper {

	private JdbcTemplate jdbcTemplate;

	/** private common fields */
	private static int NUMBERS_PER_PAGE = Constants.NUMBERS_PER_PAGE;

	private int numPerPage;
	private int totalRows;
	private int totalPages;
	private int currentPage;
	private int startIndex;
	private int endIndex;

	/**
	 * Default constructor consider the current page is the first and numPerPage
	 * is 15
	 */
	public PaginationHelper() {
		this(1, NUMBERS_PER_PAGE);
	}

	public PaginationHelper(int currentPage) {
		this(currentPage, NUMBERS_PER_PAGE);
	}

	public PaginationHelper(int currentPage, int numPerPage) {
		this.currentPage = currentPage;
		this.numPerPage = numPerPage;
	}

	/**
	 * Intital some basic fields
	 */
	public void init(String countSql) {
		setTotalRows(getJdbcTemplate().queryForInt(countSql));
		setTotalPages();
		setStartIndex();
		setEndIndex();
	}

	public void init(String countSql, int status) {
		Object[] params = new Object[] { status };
		int[] types = new int[] { Types.INTEGER };

		setTotalRows(getJdbcTemplate().queryForInt(countSql, params, types));
		setTotalPages();
		setStartIndex();
		setEndIndex();
	}

	/**
	 * Execute pagination query
	 */
	public List currentPageQuery(String countSql, String fetchDataSql)
			throws IllegalArgumentException {
		if (getJdbcTemplate() == null) {
			throw new IllegalArgumentException(
					"com.wap.framework.pagination.PaginationHelper.jdbcTemplate is null, pls check it!");
		}
		this.init(countSql);
		return getJdbcTemplate().queryForList(fetchDataSql);
	}

	/** Setters and Getters */
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public int getStartIndex() {
		return startIndex;
	}

	public void setStartIndex() {
		this.startIndex = (getCurrentPage() - 1) * getNumPerPage();
	}

	public int getEndIndex() {
		return endIndex;
	}

	public void setEndIndex() {
		this.endIndex = this.getStartIndex() + this.getNumPerPage();
	}

	public int getTotalPages() {
		return totalPages;
	}

	public void setTotalPages() {
		this.totalPages = (int) Math.ceil(getTotalRows()
				/ (double) getNumPerPage());
	}

	public int getNumPerPage() {
		return numPerPage;
	}

	public void setNumPerPage(int numPerPage) {
		this.numPerPage = numPerPage;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		if (currentPage <= 0) {
			this.currentPage = 1;
		} else if (currentPage >= this.getTotalPages()) {
			this.currentPage = this.getTotalPages();
		} else {
			this.currentPage = currentPage;
		}
	}

	public int getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}

	/**
	 * test
	 */
	public static void main(String[] args) {
		int i = 1;
		int x = 3;

		System.out.println((int) Math.ceil(i / (double) x));
	}
}
